﻿using System;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Collections.Generic;
using DataAccess;
using System.Linq.Expressions;
using DataAccess.Helpers;
using BastInfos.Controls;

namespace BastInfos.Helpers
{
	public static class HtmlHelper
	{
		public static MvcHtmlString LabelTextFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText)
		{
			return MvcHtmlString.Create(String.Format("<label for=\"{0}\">{1}</label>", ExpressionHelper.GetExpressionText((LambdaExpression)expression), labelText));
		}

		public static MvcHtmlString DropDownListWithEmpty(this System.Web.Mvc.HtmlHelper html, string name, IEnumerable<SelectListItem> selectList, string emptyText)
		{
			List<SelectListItem> _list = selectList.ToList();
			_list.Insert(0, new SelectListItem { Text = emptyText, Value = "-1" });
			return html.DropDownList(name, _list);
		}

		public static MvcHtmlString MenuItem(this System.Web.Mvc.HtmlHelper helper, string linkText, string controllerName, string actionName)
		{
			string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
			string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

			var sb = new StringBuilder();

			if (currentControllerName.Equals(controllerName, StringComparison.InvariantCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.InvariantCultureIgnoreCase))
				sb.Append("<li class=\"selected\">");
			else
				sb.Append("<li>");

			sb.Append(helper.ActionLink(linkText, actionName, controllerName));

			if (currentControllerName.Equals(controllerName, StringComparison.InvariantCultureIgnoreCase))
			{
				if (currentControllerName.Equals(CategoryType.Article.ToString(), StringComparison.InvariantCultureIgnoreCase))
					RenderCategorySubMenu(CategoryType.Article, helper, sb);
				else if (currentControllerName.Equals(CategoryType.Evenement.ToString(), StringComparison.InvariantCultureIgnoreCase))
					RenderCategorySubMenu(CategoryType.Evenement, helper, sb);
				else if (currentControllerName.Equals("Publication", StringComparison.InvariantCultureIgnoreCase))
					RenderPublicationSubMenu(helper, sb);
			}

			sb.Append("</li>");
			return MvcHtmlString.Create(sb.ToString());
		}

		private static void RenderPublicationSubMenu(System.Web.Mvc.HtmlHelper helper, StringBuilder sb)
		{
			var years = DataRepository.Get<Publication>()
				.GroupBy(p => p.CreationDate.Year)
				.OrderByDescending(g => g.Key)
				.Select(g => g.Key);

			sb.Append("<ul class=\"sub\">");
			foreach (var year in years)
			{
				sb.Append("<li>");
				sb.Append(helper.ActionLink(year.ToString(), (string)"Year", (string)"Publication", new { year = year }, new { }));
				sb.Append("</li>");
			}
			sb.Append("</ul>");
		}

		private static void RenderCategorySubMenu(CategoryType type, System.Web.Mvc.HtmlHelper helper, StringBuilder sb)
		{
			var categories = DataRepository.Get<Category>().Where(c => c.CategoryType == (byte)type);

			sb.Append("<ul class=\"sub\">");
			foreach (var category in categories)
			{
				sb.Append("<li>");
				sb.Append(helper.ActionLink(category.Description, "Category", type.ToString(), new { id = category.id_Category, description = category.Description }, new { }));
				sb.Append("</li>");
			}
			sb.Append("</ul>");
		}

		public static Calendar Calendar(this System.Web.Mvc.HtmlHelper helper, int month, int year)
		{
			return new Calendar(month, year);
		}

		public static Calendar Calendar(this System.Web.Mvc.HtmlHelper helper)
		{
			return new Calendar();
		}
	}
}